﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using jaysJourney_v01.States;

namespace jaysJourney_v01.Utils
{
    class Button
    {
        /**
         * MEMBER VARIABLES
         * */

        // Common

        protected State state;
        protected Vector2 position;
        protected Texture2D activeTexture;
        protected Texture2D passTexture;
        protected Rectangle posRectangle;
        protected SpriteBatch parentSpriteBatch;
        protected SpriteFont textFont;

        private bool active;        
        private Color color;
        private String name;
        private Vector2 txtSize;
        private bool showTxt;

        /**
         * METHODS
         * */

        //Konstruktor für automatische x-zentrierung

        public Button(State state, String name, String passTexture, String activeTexture, int yPos, Color color, bool active, bool showTxt)
            : this(state, name, passTexture, activeTexture, -1, yPos, color, active, showTxt)
        {
        }

        // Constructor with x and y position

        public Button(State state, String name, String passTexture, String activeTexture,int xPos, int yPos, Color color, bool active, bool showTxt)
        {
            this.state = state;
            this.passTexture = state.getStateManager().getGame().Content.Load<Texture2D>(passTexture);
            this.activeTexture = state.getStateManager().getGame().Content.Load<Texture2D>(activeTexture);
            this.textFont = state.getStateManager().getGame().textFont;

            if (xPos == -1) this.position = new Vector2(state.getGame().GraphicsDevice.Viewport.Width / 2 - this.passTexture.Width / 2, yPos);
            else this.position = new Vector2(xPos, yPos);
                
            this.posRectangle = new Rectangle((int)this.position.X, yPos, this.passTexture.Width, this.passTexture.Height);
            this.color = color;
            this.active = active;
            this.name = name;
            this.txtSize = this.textFont.MeasureString(this.name);
            this.showTxt = showTxt;
        }
        
        // get texture

        /**public Texture2D getTexture()
        {
            if (this.active)
            {
                return activeTexture;
            }
            else
            {
                return passTexture;
            }
        }*/

        // get color

        public Color getColor()
        {
            return this.color;
        }

        // get collision Rectangle

        public Rectangle getRectangle()
        {
            return this.posRectangle;
        }

        // set active

        public void setActive(bool active)
        {
            this.active = active;
        }

        // get active

        public bool getActive()
        {
            return this.active;
        }

        // get name

        public String getName()
        {
            return this.name;
        }

        // update ( animations for example )

        public void update() {}

        // add spritebatch

        public void addSpriteBatch(SpriteBatch sb)
        {
            this.parentSpriteBatch = sb;
        }

        // draw

        public void draw()
        {
            if(active) parentSpriteBatch.Draw(activeTexture, position, Color.White);
            else parentSpriteBatch.Draw(passTexture, position, Color.White);
        }

        // set new position

        public void setNewPosition(Vector2 newPos)
        {
            this.position = newPos;
            this.posRectangle = new Rectangle((int)this.position.X, (int)this.position.Y, this.passTexture.Width, this.passTexture.Height);
        }

        public void setNewPosition(int yPos)
        {
            this.position = new Vector2(this.state.getGame().GraphicsDevice.Viewport.Width / 2 - this.passTexture.Width / 2, yPos);
            this.posRectangle = new Rectangle((int)this.position.X, (int)this.position.Y, this.passTexture.Width, this.passTexture.Height);
        }

        public void setNewPosition(int xPos, int yPos)
        {
            this.position = new Vector2(xPos, yPos);
            this.posRectangle = new Rectangle((int)this.position.X, (int)this.position.Y, this.passTexture.Width, this.passTexture.Height);
        }

        // draw txt
        public void drawName(Color color)
        {
            this.parentSpriteBatch.DrawString(this.textFont, this.name, this.position, color, 0, new Vector2((-this.passTexture.Width / 2) + (this.txtSize.X / 2), (-this.passTexture.Height / 2) + (this.txtSize.Y / 2)), 1, SpriteEffects.None, 0);
        }

        // draw given txt
        public void drawName(Color color, String txt)
        {
            Vector2 pos = this.textFont.MeasureString(txt);
            this.parentSpriteBatch.DrawString(this.textFont, txt, this.position, color, 0, new Vector2((-this.passTexture.Width / 2) + (int)(pos.X / 2), (-this.passTexture.Height / 2) + (int)(pos.Y / 2)), 1, SpriteEffects.None, 0);
        }

        // show txt
        public bool getShowTxt()
        {
            return this.showTxt;
        }
    }
}
